10 December 2020

About this Document

This is an R Markdown presentation and it is created using Rstudio.

RStudio is an integrated development environment (IDE) for R. RStudio is available in open source and commercial editions and runs on the desktop (Windows, Mac, and Linux).

R is a language and environment for statistical computing and graphics. R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …) and graphical techniques, and is highly extensible. R is available as Free Software.

Steps

  • Determine the libraries to use
  • Read the file and transform the data
  • Select the more appropriate kind of chart to represent the information
  • Create a dynamic time series chart

Libraries Used

R packages are a collection of R functions, compiled code and sample data. They are stored under a directory called “library” in the R environment. By default, R installs a set of packages during installation. More packages are added later when they are needed for some specific purpose.

library(readxl) #Read the data from excel sheet
library(DT) #Show a dynamic table
library(ggplot2) #Create Charts
library(xts) #Create Time Series
library(highcharter) #Represent the data in timeseries chart
library(imager) #Open image

Load the Table and any other Changes in Data

#Load the table, establishing the sheet, range and column types of the data

Turtle <- read_excel("Turtles.xlsx", 
                     sheet = "Chart", range = cell_cols("A:C"), 
                     col_types = c("date","numeric", "numeric")) 

#Change column names

names(Turtle)[2] <- "egglaying"
names(Turtle)[3] <- "nharv"

Create a visualization of the Table

table <- datatable(Turtle, extensions = c('Buttons','FixedColumns', 'RowReorder'),
  filter = list(position = 'top', clear = FALSE),
  options = list(dom = 'Bfrtip',
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print'), dom = 't',
    scrollX = TRUE,
    fixedColumns = TRUE,
    rowReorder = TRUE, order = list(c(0 , 'asc'))))

Consult the Table

Summary of the data

summary(Turtle[,2:3])
##    egglaying          nharv        
##  Min.   :  2044   Min.   :  4.042  
##  1st Qu.: 13544   1st Qu.:  8.735  
##  Median : 37309   Median : 15.709  
##  Mean   : 65980   Mean   : 21.406  
##  3rd Qu.: 74106   3rd Qu.: 28.998  
##  Max.   :477342   Max.   :100.000

Create the Time Series Chart

#Create chart using ggplot tools

Chart_1 <- Turtle %>% ggplot() + 
  geom_bar(mapping = aes(x = as.Date(Date, "%Y-%m", tz = "UTC"), y = egglaying, group = 2, size = "Eggs Laying"), 
           stat = "identity", colour = "#2F528F", fill = "#2F528F") +
  scale_size_manual(NULL, values = 0.5) +
  geom_point(mapping = aes(x = as.Date(Date, "%Y-%m", tz = "UTC"), 
            y = nharv * 500000/100, group = 1, color = "Nest harvested"), size = 3, 
            shape = 21, fill = "#ED7D31", inherit.aes = FALSE) +
  geom_line(mapping = aes(x = as.Date(Date, "%Y-%m", tz = "UTC"), 
            y = nharv * 500000/100, group = 1, color = "Nest harvested"), colour="#ED7D31", inherit.aes = FALSE) +
  scale_color_manual(NULL, values = "#ED7D31") +
  scale_x_date(date_labels = "%b-%Y", date_breaks = "6 months") +
  scale_y_continuous(
    name = "Egg Laying Females (total number)", 
    sec.axis = sec_axis(~. * 100/500000, name="Nests Harvested (percent)"),
    limits = c(0, 500000)) +  xlab("Date") +
  ggtitle("Total number of Egg Laying Females and Percent of Nests Harvested
       Ostional - Costa Rica, Period 2006-2010") + theme_bw() +
  theme(panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    legend.position="top", 
    legend.background = element_rect(fill = "transparent"), 
    legend.box.background = element_rect(fill = "transparent", colour = NA),
    legend.key = element_rect(fill = "transparent"), 
    legend.spacing = unit(-1, "lines"))

Time Series Chart

Create the Time Series Vectors

A time series chart, also called a times series graph or time series plot, is a data visualization tool that illustrates data points at successive intervals of time. Each point on the chart corresponds to both a time and a quantity that is being measured.

# Change dataset to Time Series format

Turtle.ts.eggs <- xts(Turtle$egglaying, Turtle$Date)
Turtle.ts.nest <- xts(Turtle$nharv, Turtle$Date)

Create the Time Series Dynamic Chart

#Create the Time Series Chart

Chart_2 <- highchart(type = "stock") %>% 
  hc_title(text = "Total number of Egg Laying Females and Percent of Nests Harvested
       <br>Ostional - Costa Rica, Period 2006-2010</br>") %>% 
  hc_subtitle(text = "Figure Adapted from: Valverde et al. 2012") %>% 
  hc_yAxis_multiples(list(title = list(text = "Egg Laying Females (total number)"), opposite = FALSE),
      list(showLastLabel = FALSE, opposite = TRUE, title = list(text = "Nests Harvested (percent)"))) %>% 
  hc_add_series(Turtle.ts.eggs,  yAxis = 0, type = "column", 
      pointInterval= 30*24*3600*1000, name="Egg Laying Females", color = "#2F528F")%>%
  hc_add_series(Turtle.ts.nest,  yAxis = 1, type = "spline", 
      pointInterval= 30*24*3600*1000, name="Percentage of Nests Harvested", color = "#ED7D31")%>%
  hc_plotOptions(spline = list(marker = list(enabled = TRUE, radius = 4))) %>% 
  hc_xAxis(title= list(text='Date'), type='datetime')

Time Series Dynamic Chart

Figure Adapted from: Valverde et al. 2012

im <- load.image("chart2.jpg")
im %>% plot(axes=F)